package cn.kawa.basic.controller;

import cn.kawa.basic.dto.SmsDto;
import cn.kawa.basic.exception.BusinessException;
import cn.kawa.basic.service.IVerifyCodeService;
import cn.kawa.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;


    @GetMapping("/image/{key}")
    public JsonResult getverifyCode(@PathVariable("key") String key){
        try {
            String baseStr = verifyCodeService.getVerifyCode(key);
            return JsonResult.me().setResultObj(baseStr);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("获取验证码失败");
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("获取验证码失败");
        }

    }

    @PostMapping("/smsCode")
    public JsonResult smsCode(@RequestBody SmsDto smsDto){
        try {
             verifyCodeService.sendSmsCode(smsDto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("短信发送异常："+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("获取验证码失败");
        }

    }

    @PostMapping("/binderSmsCode")
    //此处用smsDto 只利用其中的phone参数即可，发送验证码
    public JsonResult binderSmsCode(@RequestBody SmsDto smsDto){
        try {
            verifyCodeService.binderSmsCode(smsDto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("短信发送异常："+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("获取验证码失败");
        }

    }


}
